//
//  ST_AIMycenterHeaderView.swift
//  btvideo
//
//  Created by mac on 2020/3/13.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyJSON
import SwiftyUserDefaults
import RealmSwift
import RxCocoa
import NSObject_Rx
import RxSwift


/**
 人脸头像认证状态(1:未认证,2:人脸认证,头像不匹配,3:人脸头像匹配,4:审核中)
 */
var FACE_AVATAE_STATE = 1{
    didSet{
        
        NotificationCenter.default.post(name: Notification.Name.face_status, object: nil)
        
    }
}


func AVATAR_SUPPLEMENT(guardTime:Int,guardContent:String) -> String? {
    
    if guardTime.double > Date().timeIntervalSince1970{
        return guardContent
    }else{
        return nil
    }
}


class ST_AIMycenterHeaderView: UICollectionReusableView {
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(90)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(60+statusBarHeight)
        }
        
        self.addSubview(i_am_not_label)
        i_am_not_label.snp.makeConstraints { (make) in
            make.edges.equalTo(icon_image_view)
        }
        
        self.addSubview(icon_supplement_image_view)
        icon_supplement_image_view.snp.makeConstraints { (make) in
            make.center.equalTo(icon_image_view)
            make.width.height.equalTo(120)
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(push_my_center_handle))
        icon_image_view.isUserInteractionEnabled = true
        icon_image_view.addGestureRecognizer(tap)
        
        
        
        self.addSubview(user_name_label)
        user_name_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon_image_view.snp.bottom).offset(10)
            make.height.equalTo(33)
        }
        
        self.addSubview(member_sign_image_view)
        member_sign_image_view.snp.makeConstraints { (make) in
            make.centerY.equalTo(user_name_label)
            make.left.equalTo(user_name_label.snp.right).offset(5)
        }
        
        self.addSubview(goddess_sign_image_view)
        goddess_sign_image_view.snp.makeConstraints { (make) in
            make.centerY.equalTo(user_name_label).offset(-5)
            make.left.equalTo(user_name_label.snp.right).offset(5)
            make.width.equalTo(70/2.0)
            make.height.equalTo(60/2.0)
        }
        
        
        
        
        self.addSubview(detective_btn)
        detective_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(0)
            make.top.equalTo(user_name_label.snp.bottom).offset(10)
        }
        self.addSubview(detective_btn_right_image_view)
        detective_btn_right_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(detective_btn.snp.right).offset(-2)
            make.centerY.equalTo(detective_btn)
        }
        binding()
        
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self)
    }
    
    fileprivate lazy var value_btns = [UIView]()
    
    fileprivate lazy var segment_btns = [UIView]()
    
    private func binding(){
        
        NotificationCenter.default.rx.notification(Notification.Name.face_status, object: nil).subscribe(onNext: { _ in
        
            self.modify_face_status_handle()
            
        }).disposed(by: rx.disposeBag)
        
        
        
        Defaults.rx.observe(Bool.self, DefaultsKeys.isPanda._key).subscribe(onNext: { f in
            
            self.value_btns.forEach({ $0.removeFromSuperview() })
            /// 必须配置为 FALSE
            if let b = f ,b == false{
                
                let littleValueViewWidth = (ScreenWidth - 10 * 3 - 15 * 2)/3.0
                
                self.value_btns = [self.left_little_value_view,self.medium_little_value_view,self.right_little_value_view]
                
                self.value_btns.forEach({ self.addSubview($0) })
                
                self.left_little_value_view.snp.makeConstraints { (make) in
                    make.left.equalToSuperview().offset(15)
                    make.height.equalTo(61)
                    make.width.equalTo(littleValueViewWidth)
                    make.top.equalTo(self.user_name_label.snp.bottom).offset(44)
                }
                
                self.medium_little_value_view.snp.makeConstraints { (make) in
                    make.centerX.equalToSuperview()
                    make.centerY.width.height.equalTo(self.left_little_value_view)
                }
                
                
                self.right_little_value_view.snp.makeConstraints { (make) in
                    make.right.equalToSuperview().offset(-15)
                    make.centerY.width.height.equalTo(self.left_little_value_view)
                }
                
                
            }else{
                
                let littleValueViewWidth = (ScreenWidth - 15 * 3)/2.0
                
                self.value_btns = [self.medium_little_value_view,self.right_little_value_view]
                
                self.value_btns.forEach({ self.addSubview($0) })
                
                self.medium_little_value_view.snp.makeConstraints { (make) in
                    make.left.equalToSuperview().offset(15)
                    make.height.equalTo(61)
                    make.width.equalTo(littleValueViewWidth)
                    make.top.equalTo(self.user_name_label.snp.bottom).offset(44)
                }
                
                
                self.right_little_value_view.snp.makeConstraints { (make) in
                    make.right.equalToSuperview().offset(-15)
                    make.height.equalTo(61)
                    make.width.equalTo(self.medium_little_value_view)
                    make.top.equalTo(self.user_name_label.snp.bottom).offset(44)
                }
                
            }
            
            
            
        }).disposed(by: rx.disposeBag)
        
        
        
        self.segment_btns = [self.peculiarity_tab_btn,self.knighthood_tab_btn,self.face_status_tab_btn,self.member_tab_btn]
        
        self.last_tab_btn?.isSelected = false
        self.peculiarity_tab_btn.isSelected = true
        self.last_tab_btn = self.peculiarity_tab_btn
        
        self.segment_btns.forEach({ self.addSubview($0) })
      
        
        let tabOffset = 0
        
        self.knighthood_tab_btn.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(35)
            make.right.equalTo(self.snp.centerX).offset(0)
            make.bottom.equalToSuperview().offset(-5)
        }
        
        self.face_status_tab_btn.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.centerX).offset(0)
            make.width.height.centerY.equalTo(self.knighthood_tab_btn)
        }
        
        
        
        self.peculiarity_tab_btn.snp.makeConstraints { (make) in
            make.right.equalTo(self.knighthood_tab_btn.snp.left).offset(-tabOffset)
            make.width.height.centerY.equalTo(self.knighthood_tab_btn)
        }
        
        self.member_tab_btn.snp.makeConstraints { (make) in
            make.width.height.centerY.equalTo(self.knighthood_tab_btn)
            make.left.equalTo(self.face_status_tab_btn.snp.right).offset(tabOffset)
        }
        
        
        
        Defaults.rx.observe(Bool.self, DefaultsKeys.isPanda._key).withUnretained(self).subscribe(onNext: {own,_ in
            
            own.setting_mediumcontrol_value()
            
        }).disposed(by: rx.disposeBag)
        
        
        SCComponets.rich_text_storage_component.pulled_observable.withUnretained(self).subscribe(onNext: { own,_ in
            
            own.setting_mediumcontrol_value()
            
        }).disposed(by: rx.disposeBag)
        

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setting_mediumcontrol_value(){
        
        if Defaults[.isPanda]{
            medium_little_value_view.data = (title:"我的积分",imageName:"个人中心shouy_small",unitStr:"\(Defaults[.PandaStr])")
        }else{
            medium_little_value_view.data = (title:"我的积分",imageName:SCComponets.rich_text_storage_component.json["ai_my_center"]["mid_url"].stringValue,unitStr:"\(Defaults[.PandaStr])")
        }
    }
    
    //    MARK: - Properties
    fileprivate lazy var icon_image_view:ST_PairsCenterPeculiarityImageView = {
        let control = ST_PairsCenterPeculiarityImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 90/2.0
        return control
    }()
    
//    fileprivate lazy var icon_supplement_image_view:UIImageView = {
//        let control = UIImageView()
//        return control
//    }()
    
    fileprivate lazy var icon_supplement_image_view:ST_AvatarSupplementView = {
        let control = ST_AvatarSupplementView()
        return control
    }()
    
    fileprivate lazy var i_am_not_label:UILabel = {
        let control = UILabel.init()
        control.text = "非本人头像"
        control.textColor = Color.white
        control.textAlignment = .center
        control.backgroundColor = Color.init(white: 0, alpha: 0.4)
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.cornerRadius = 90/2.0
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var user_name_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 24, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var detective_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        let space = "     "
        control.setTitleForAllStates("真实头像未认证"+space)
        control.setTitle("真人头像已认证"+space, for: UIControl.State.selected)
        control.setTitleColorForAllStates(UIColor.init(hexString: "#61666F")!)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.setImage(ST_ImageUtils.imageName("个人中心_icon_真人认证")!, for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("个人中心_icon_真人认证sel")!, for: UIControl.State.selected)
        control.addTarget(self, action: #selector(detect_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var detective_btn_right_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("个人中心_icon_向后一页")!)
        control.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(detect_handle))
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var tab_normal_image:UIImage = {
        let img = UIImage.init(color: UIColor.white, size: CGSize.init(width: 1, height: 1))
        return img
    }()
    
    fileprivate lazy var tab_selected_image:UIImage = {
        let img = UIImage.init(color: UIColor.init(hex: 0xFEC207)!, size: CGSize.init(width: 1, height: 1))
        return img
    }()
    
//    medium_little_value_view
    
    
    
    fileprivate lazy var medium_little_value_view:ST_MyCenterLittleValueItemControl = {
        let control = ST_MyCenterLittleValueItemControl()
        control.addTarget(self, action: #selector(ear_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var left_little_value_view:ST_MyCenterLittleValueItemControl = {
        let control = ST_MyCenterLittleValueItemControl()
        
        control.data = (title:"红包",imageName:"ai_my_center_red_packet_icon",unitStr:"个")

        control.addTarget(self, action: #selector(daily_task_handle(sender:)), for: UIControl.Event.touchUpInside)

        return control
    }()
    
    fileprivate lazy var right_little_value_view:ST_MyCenterLittleValueItemControl = {
        let control = ST_MyCenterLittleValueItemControl()
        control.data = (title:"我的甜心币",imageName:"个人主页甜心币_small",unitStr:"个")
        control.addTarget(self, action: #selector(push_coin_controller), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    /*
     fileprivate lazy var followFansView:ST_FollowFansView = {
     let control = ST_FollowFansView()
     return control
     }()
     */
    
    fileprivate var baseTag = 1022
    
    fileprivate lazy var peculiarity_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("特质")
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.setTitleColor(UIColor.init(hex: 0x333333)!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.cornerRadius = 15
        control.setBackgroundImage(tab_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(tab_normal_image, for: UIControl.State.normal)
        control.tag = baseTag
        control.addTarget(self, action: #selector(switch_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var knighthood_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("贵族")
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.setTitleColor(UIColor.init(hex: 0x333333)!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.cornerRadius = 15
        control.setBackgroundImage(tab_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(tab_normal_image, for: UIControl.State.normal)
        control.tag = baseTag + 1
        control.addTarget(self, action: #selector(switch_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var face_status_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("认证")
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.setTitleColor(UIColor.init(hex: 0x333333)!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.cornerRadius = 15
        control.setBackgroundImage(tab_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(tab_normal_image, for: UIControl.State.normal)
        control.tag = baseTag + 2
        control.addTarget(self, action: #selector(switch_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var member_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("会员")
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.setTitleColor(UIColor.init(hex: 0x333333)!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.cornerRadius = 15
        control.setBackgroundImage(tab_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(tab_normal_image, for: UIControl.State.normal)
        control.tag = baseTag + 3
        control.addTarget(self, action: #selector(switch_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    
    fileprivate lazy var member_sign_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("首页__会员标示_大")!)
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var goddess_sign_image_view:UIImageView = {
        let control = UIImageView.init(image: SC.YYGoddessSignImage)
        control.isHidden = true
        return control
    }()
    
    
    fileprivate var last_tab_btn:UIButton?
    
    fileprivate var displayed_change_avatar = false
    
    //    MARK: - Closure
    var switch_tab_closure:((_ index:Int)->())?
    
    var ask_refresh_closure:(()->())?
    //    MARK: - Bind Model
    var json = JSON.null{
        didSet{
            
            FACE_AVATAE_STATE = json["face_avatar_state"].intValue
            
            if FACE_AVATAE_STATE == 2 && displayed_change_avatar == false{
                
                displayed_change_avatar = true
                
                main_thread_execute(afterDelay: 3) {
                    
                    let alert = ST_BTChangeAvatarAlertController()
                    
                    alert.overCurrentContext = true
                    
                    alert.over_current_context_alpha = "0.4"
                    
                    GlobalPresent(toController: alert, animated: false)
                }
            }
            
            
            icon_image_view.pin_setImage(from: json["avatar"].stringValue.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
//            icon_supplement_image_view.pin_setImage(from: AVATAR_SUPPLEMENT(guardTime: json["guard_time"].intValue, guardContent: json["guard_content"].stringValue)?.url)
            
            icon_supplement_image_view.bind(gender: ST_BTUserInfoManager.shared.current_account_info?.userinfo?.gender ?? 0, url: AVATAR_SUPPLEMENT(guardTime: json["guard_time"].intValue, guardContent: json["guard_content"].stringValue))
            
            if let str = json["nickname_auditing"].string,!str.isEmpty{
                user_name_label.text = str
            }else{
                user_name_label.text = json["nickname"].stringValue
            }
            
            
            
            modify_face_status_handle()
            
            
            let userInfo = ST_BTUserInfoManager.shared.current_account_info
            
            let realm = try! Realm()
            
            try! realm.write {
                userInfo?.userinfo?.avatar = json["avatar"].stringValue
            }
            
            ST_BTLaunchController.shared.update_mycenter_tab_avatar()
            
            refresh_member_sign()
            
            refresh_little_item_view_value()
            
            //            followFansView.model = ST_FollowFansViewModel.init(fans: json["fan_num"].intValue, follow: json["attention_num"].intValue)
            
        }
    }
    
    func refresh_member_sign(){
        
        if json["is_goddess"].boolValue{
            goddess_sign_image_view.isHidden = false
            member_sign_image_view.isHidden = true
        }else if FETCH_MEMBER_STATUS() == .subscribe{
            goddess_sign_image_view.isHidden = true
            member_sign_image_view.isHidden = false
        }else{
            goddess_sign_image_view.isHidden = true
            member_sign_image_view.isHidden = true
        }
    }
    
    
    @objc func modify_face_status_handle(){
        
        let space = "     "
        
        switch FACE_AVATAE_STATE {
        case 1:
            
            detective_btn.snp.updateConstraints { (make) in
                make.centerX.equalToSuperview().offset(-9)
            }
            detective_btn_right_image_view.isHidden = false
            i_am_not_label.isHidden = true
            detective_btn.isSelected = false
            icon_image_view.isCheck = false
        case 2:
            
            detective_btn.snp.updateConstraints { (make) in
                make.centerX.equalToSuperview().offset(0)
            }
            detective_btn_right_image_view.isHidden = true
            i_am_not_label.isHidden = false
            
            detective_btn.setTitle("真人识别已认证"+space, for: UIControl.State.selected)
            detective_btn.isSelected = true
            icon_image_view.isCheck = false
        case 3:
            detective_btn.snp.updateConstraints { (make) in
                make.centerX.equalToSuperview().offset(0)
            }
            detective_btn_right_image_view.isHidden = true
            i_am_not_label.isHidden = true
            detective_btn.setTitle("真人头像已认证"+space, for: UIControl.State.selected)
            detective_btn.isSelected = true
            icon_image_view.isCheck = false
        case 4: //审核中
            
            detective_btn.snp.updateConstraints { (make) in
                make.centerX.equalToSuperview().offset(0)
            }
            detective_btn_right_image_view.isHidden = true
            //            i_am_not_label.isHidden = false
            detective_btn.setTitle("真人识别已认证"+space, for: UIControl.State.selected)
            detective_btn.isSelected = true
            
            icon_image_view.isCheck = true
            i_am_not_label.isHidden = true
        default:
            detective_btn.snp.updateConstraints { (make) in
                make.centerX.equalToSuperview().offset(0)
            }
            detective_btn_right_image_view.isHidden = false
            i_am_not_label.isHidden = true
            detective_btn.setTitleForAllStates("真实头像未认证"+space)
            detective_btn.isSelected = false
            icon_image_view.isCheck = false
        }
        
        
    }
    
}

//MARK: - Event
extension ST_AIMycenterHeaderView{
    
    @objc fileprivate func push_my_center_handle()  {
        
        
        
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = Defaults[.currentUserID]
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    @objc func push_coin_controller(){
        
        
        self.parentViewController?.navigationController?.pushViewController(ST_RechargeViewController(), animated: true)
        
    }
    
//    @objc func push_gift_controller(){
//
//        self.parentViewController?.navigationController?.pushViewController(ST_GiftRankViewController(), animated: true)
//
//    }
    
    func refresh_little_item_view_value(){
        
        self.right_little_value_view.numbervalue = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sweet_coin.int ?? 0
        
        self.medium_little_value_view.numbervalue = json["chum_num"].intValue
        
        guard let income = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.income else { return
            
            self.medium_little_value_view.number_doublevalue = 0
            
        }
        
        self.medium_little_value_view.number_doublevalue = income
        
        self.left_little_value_view.numbervalue = json["red_packet_count"].intValue
    }
    
    //    func setCoin(value:Float)  {
    //
    //        self.right_little_value_view.numbervalue = value.int
    //
    //    }
    
    //    func setFriendsCount(value:Int)  {
    //
    //        self.left_little_value_view.numbervalue = value
    //
    //    }
    
    //    func refresh_income_value()  {
    //    }
    
    @objc fileprivate func ear_handle(sender:UIButton)  {
        
        if Defaults[.isPanda]{

            self.parentViewController?.navigationController?.pushViewController(ST_ConversionListViewController())

        }else{

            self.parentViewController?.navigationController?.pushViewController(ST_BitianValueController())
        }
    }
    
    
    @objc fileprivate func daily_task_handle(sender:UIButton)  {
        
        
        let vc = ST_TianDailyTaskController.generator()
        
        vc.dailyTaskAskRefresh.withUnretained(self).subscribe(onNext: { own,_ in
            own.ask_refresh_closure?()
        }).disposed(by: rx.disposeBag)
        
        self.parentViewController?.navigationController?.pushViewController(vc)
    }
    
    @objc fileprivate func switch_handle(sender:UIButton)  {
        
        if sender.isSelected{
            return
        }
        
        last_tab_btn?.isSelected = false
        last_tab_btn = sender
        last_tab_btn?.isSelected = true
        
        let idx = sender.tag - self.baseTag
        switch_tab_closure?(idx)
    }
    
    @objc func detect_handle()  {
        
        
        
        
        
        
        if FACE_AVATAE_STATE == 2{
            
            let failure = ST_MatchFaceFailureController()
            
            failure.overCurrentContext = true
            
            GlobalPresent(toController: failure)
            
            return
            
        }
        
        if FACE_AVATAE_STATE == 1{
            DetectFaceHandle()
        }
        
    }
}

//MARK: - View
fileprivate class ST_FollowFansView: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(left_label)
        self.addSubview(center_label)
        self.addSubview(right_label)
        
        left_label.snp.makeConstraints { (make) in
            make.left.centerY.equalToSuperview()
        }
        
        center_label.snp.makeConstraints { (make) in
            make.left.equalTo(left_label.snp.right)
            make.centerY.equalToSuperview()
        }
        
        right_label.snp.makeConstraints { (make) in
            make.left.equalTo(center_label.snp.right)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        /*
         self.addSubview(left_btn)
         self.addSubview(right_btn)
         
         left_btn.snp.makeConstraints { (make) in
         make.left.equalTo(left_label)
         make.top.bottom.equalToSuperview()
         make.right.equalTo(center_label.snp.centerX)
         }
         
         right_btn.snp.makeConstraints { (make) in
         make.right.equalTo(right_label)
         make.top.bottom.equalToSuperview()
         make.left.equalTo(center_label.snp.centerX)
         }*/
        
        self.addTarget(self, action: #selector(center_handle), for: UIControl.Event.touchUpInside)
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var left_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#61666F")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var center_label:UILabel = {
        let control = UILabel.init()
        control.text = " · "
        control.textColor = Color.init(hexString: "#61666F")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var right_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#61666F")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var left_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(left_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var right_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(right_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    var model:ST_FollowFansViewModel?{
        didSet{
            
            guard let m = model else { return }
            
            
            center_label.text = "我喜欢的 " + m.follow.st_number_description()
            
            //            left_label.text = m.fans.st_number_description() + "粉丝"
            //
            //            right_label.text = m.follow.st_number_description() + "关注"
            
        }
    }
    
    
    @objc fileprivate func center_handle(){
        
        
        self.parentViewController?.navigationController?.pushViewController(ST_MyFollowViewController())
    }
    
    @objc fileprivate func right_handle(){
        
        
        self.parentViewController?.navigationController?.pushViewController(ST_MyFollowViewController())
    }
    
    @objc fileprivate func left_handle(){
        
        self.parentViewController?.navigationController?.pushViewController(ST_MyFansViewController())
        
    }
    
}

struct ST_FollowFansViewModel {
    var fans:Int
    var follow:Int
}





//MARK: - Match Face

func DetectFaceHandle(){
    
    
    if ST_BTUserInfoManager.shared.app_chat_status_behavior.value.rawValue != 0  || ST_TIANEngagementManager.defaultManager.engagement_entiy != nil {
        
        
        TopmostViewController()?.toast(text: "当前不能进行此操作")
        
        return
    }
    
    GlobalPresent(toController: ST_DetectFaceBeforeController(), animated: false)
    
    ST_BTGlobalDataCenter.shared.start_detect_face_event()
    
}

func restrictFemaleFace()->Bool{
    
    if let restriction = ST_BTUserInfoManager.shared.additional_attributes?.faceRestricted,restriction == true,FACE_AVATAE_STATE != 3{
        
        ToastDetectFaceTips {
            
            let alert = BTVerticalAlertViewController.createDetectFaceForTalkAlert(title: "通过真人认证才能使用", content: "完成了真人头像认证后就可以\n发送消息，发布特质，发布评论。", confirmStr: "立即认证", cancelStr: "取消",viewHeight:372)
            
            
            alert.confirm_closure = {
                
                TIANFaceLogicHandle(controller: TopmostViewController())
            }
            
            return alert
        }
        
        return true
    }
    
    return false
}





func TIANFaceLogicHandle(controller:UIViewController?){

    switch FACE_AVATAE_STATE {
    case 2:
        let failure = ST_MatchFaceFailureController()
                            
        failure.overCurrentContext = true
        
        GlobalPresent(toController: failure)
    case 3:
        break
    case 4:
        controller?.toast(text: "真人认证审核中,请稍后...")
    default:
        DetectFaceHandle()
    }
}

